﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using HF.Framework.DataClientDBAgent;
using HF.WorkFlow.DataContract;
using HF.Framework.SqlDataContract;
using HF.WorkFlow.Const;
namespace HF.WorkFlow.Template
{
    /// <summary>
    /// 数据访问类WorkLineData。
    /// </summary>
    public class WorkLine
    {
        #region  私有字段
        private SqlDataItem sqlDataItem = new SqlDataItem();
        private string[] fieldList = { "WorkLineId", "WorkFlowId", "StartTaskId", "EndTaskId", "Condition", "Description", "BreakX", "BreakY", "CommandName", "Priority", "startLx", "startLy", "endLx", "endLy" };
        private string tableName = "HF_WorkLine";
        private string keyField = "WorkLineId";
        private string sqlString = "";

        #endregion

        #region  私有方法
        private void setParameter(WorkLineEntity entity)
        {
            sqlDataItem.ParameterList.Clear();
            sqlDataItem.AppendParameter("@WorkLineId", entity.WorkLineId, typeof(string));
            sqlDataItem.AppendParameter("@WorkFlowId", entity.WorkFlowId, typeof(string));
            sqlDataItem.AppendParameter("@StartTaskId", entity.StartTaskId, typeof(string));
            sqlDataItem.AppendParameter("@EndTaskId", entity.EndTaskId, typeof(string));
            sqlDataItem.AppendParameter("@Condition", entity.Condition, typeof(string));
            sqlDataItem.AppendParameter("@Description", entity.Description, typeof(string));
            sqlDataItem.AppendParameter("@BreakX", entity.BreakX, typeof(string));
            sqlDataItem.AppendParameter("@BreakY", entity.BreakY, typeof(string));
            if (string.IsNullOrEmpty(entity.CommandName)) entity.CommandName = WorkFlowConst.DefaultCommand;//默认提交
            sqlDataItem.AppendParameter("@CommandName", entity.CommandName, typeof(string));
            sqlDataItem.AppendParameter("@Priority", entity.Priority, typeof(int));
            sqlDataItem.AppendParameter("@startLx", entity.startLx, typeof(int));
            sqlDataItem.AppendParameter("@startLy", entity.startLy, typeof(int));
            sqlDataItem.AppendParameter("@endLx", entity.endLx, typeof(int));
            sqlDataItem.AppendParameter("@endLy", entity.endLy, typeof(int));
        }

        private void setInsertSql()
        {
            string tmpValueList = "";
            string tmpFieldName = "";
            sqlString = "insert into " + tableName + "(";
            int tmpInt = this.fieldList.Length;
            for (int i = 0; i < tmpInt - 1; i++)
            {
                tmpFieldName = fieldList[i].ToString();
                sqlString = sqlString + tmpFieldName + ", ";
                tmpValueList = tmpValueList + "@" + tmpFieldName + ",";
            }
            tmpFieldName = this.fieldList[tmpInt - 1].ToString();
            sqlString = sqlString + tmpFieldName;
            tmpValueList = tmpValueList + "@" + tmpFieldName;
            this.sqlString = sqlString + ")values(" + tmpValueList + ")";
            sqlDataItem.CommandText = sqlString;
        }

        private void setUpdateSql()
        {
            string tmpFieldName = "";
            int tmpInt = this.fieldList.Length;
            sqlString = "update " + tableName + " set ";
            for (int i = 0; i < tmpInt - 1; i++)
            {
                tmpFieldName = fieldList[i].ToString();
                sqlString = sqlString + tmpFieldName + " = @" + tmpFieldName + ", ";
            }
            tmpFieldName = this.fieldList[tmpInt - 1].ToString();
            sqlString = sqlString + tmpFieldName + " = @" + tmpFieldName;
            sqlString = sqlString + " where " + keyField + "=@" + this.keyField;
            sqlDataItem.CommandText = sqlString;
        }

        public WorkLineEntity GetWorkLineEntity(DataRow dr)
        {
            WorkLineEntity entity = new WorkLineEntity();
            string value = "";
            entity.WorkLineId = dr["WorkLineId"].ToString();
            entity.WorkFlowId = dr["WorkFlowId"].ToString();
            entity.StartTaskId = dr["StartTaskId"].ToString();
            entity.EndTaskId = dr["EndTaskId"].ToString();
            entity.Condition = dr["Condition"].ToString();
            entity.Description = dr["Description"].ToString();
            entity.BreakX = dr["BreakX"].ToString();
            entity.BreakY = dr["BreakY"].ToString();
            entity.CommandName = dr["CommandName"].ToString();
            value = dr["Priority"].ToString();

            entity.startLx = dr["startLx"].ToString() != "" ? Convert.ToInt32(dr["startLx"]) : 0;


            entity.startLy = dr["startLy"].ToString() != "" ? Convert.ToInt32(dr["startLy"]) : 0;
            entity.endLx = dr["endLx"].ToString() != "" ? Convert.ToInt32(dr["endLx"]) : 0;
            entity.endLy = dr["endLy"].ToString() != "" ? Convert.ToInt32(dr["endLy"]) : 0;
            if (string.IsNullOrEmpty(value) == false)
            {
                entity.Priority = Convert.ToInt32(value);
            }
            return entity;
        }

        #endregion

        #region  构造函数
        public WorkLine()
        { }
        public static WorkLine GetInstance()
        {
            return new WorkLine();
        }
        #endregion

        #region  公有方法
        #region  基本方法
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="entity">数据实体</param>
        /// <returns>影响的记录条数</returns>
        public int Insert(WorkLineEntity entity)
        {
            try
            {
                setInsertSql();//设定insert语句
                setParameter(entity);//设定参数
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteNonQuery(sqlDataItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity">数据实体</param>
        /// <returns>影响的记录条数</returns>
        public int Update(WorkLineEntity entity)
        {
            try
            {
                setUpdateSql();//设定insert语句
                setParameter(entity);//设定参数
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteNonQuery(sqlDataItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string workLineId)
        {
            string tmpSql = "select * from " + tableName + " where " + keyField + "=@" + keyField;
            SqlDataItem sqlItem = new SqlDataItem();
            sqlItem.CommandText = tmpSql;
            sqlItem.AppendParameter("@" + keyField, workLineId);
            ClientDBAgent agent = new ClientDBAgent();
            return agent.RecordExists(sqlItem);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="workLineId">主键Id</param>
        /// <returns>影响的记录条数</returns>
        public void Delete(string workLineId)
        {
            try
            {
                string tmpSql = "delete from " + tableName + " where  " + keyField + "=@" + keyField;
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpSql;
                sqlItem.AppendParameter("@" + keyField, workLineId);
                ClientDBAgent agent = new ClientDBAgent();
                agent.ExecuteNonQuery(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取一条数据记录
        /// </summary>
        /// <param name="WorkLineId">主键Id</param>
        /// <returns>DataTable</returns>
        public DataTable GetWorkLineTable(string workLineId)
        {
            string tmpSql = "select * from " + tableName + " where " + keyField + "=@" + keyField;
            try
            {
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpSql;
                sqlItem.AppendParameter("@" + keyField, workLineId);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取一个数据实体
        /// </summary>
        /// <param name="workLineId">主键Id</param>
        /// <returns>DataTable</returns>
        public WorkLineEntity GetWorkLineEntity(string workLineId)
        {
            DataTable dt = GetWorkLineTable(workLineId);
            if (dt != null && dt.Rows.Count > 0)
            {
                return GetWorkLineEntity(dt.Rows[0]);
            }
            return null;
        }

        #endregion

        #region  扩展方法
        //此处添加扩展方法

        /// <summary>
        /// 获得流程模板的所有连线
        /// </summary>
        /// <param name="workflowId">流程Id</param>
        /// <returns>DataTable</returns>
        public  DataTable GetWorkLines(string workFlowId)
        {
            try
            {
                string tmpStr = "select * from HF_WorkLine where WorkFlowId = @workflowId";
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpStr;
                sqlItem.AppendParameter("@workflowId", workFlowId);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获得流程模板的所有连线
        /// </summary>
        /// <param name="workFlowId">流程Id</param>
        /// <returns>Entitys</returns>
        public List<WorkLineEntity> GetWorkLineEntitys(string workFlowId)
        {
            try
            {
                List<WorkLineEntity> result = new List<WorkLineEntity>();
                DataTable dt = GetWorkLines(workFlowId);
                foreach (DataRow dr in dt.Rows)
                {
                    WorkLineEntity entity = GetWorkLineEntity(dr);
                    result.Add(entity);
                }
                return result;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获取流程的所有任务连线视图
        /// </summary>
        /// <param name="workFlowId"></param>
        /// <returns></returns>
        public DataTable GetWorkTaskLineView(string workFlowId)
        {
            try
            {
                string tmpStr = "select * from HF_WorkTaskLineView where WorkFlowId = @workflowId";
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpStr;
                sqlItem.AppendParameter("@workflowId", workFlowId);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
        #endregion
    }
}

